import _turf from '@/utils/turfUtil'
import { useAirStore } from '@/stores'
import { storeToRefs } from 'pinia'
const airStore = useAirStore()
const { FocusAirId } = storeToRefs(airStore)

class AirAnimation {
  constructor() {
    this.airList = [] //所有航班
    this.polylineArr = [] // 飞行过的路径
  }

  /**参数示例
    {
      "1581F5BKD234C00CVW69": {
          "flightTime": 0,
          "flyType": "1",
          "gs": 83.33,
          "height": 1,
          "latitude": "22.58296",
          "longitude": "114.26669",
          "registerBody": "赛宝低空通航实验室",
          "snId": "1581F5BKD234C00CVW69",
          "timeStamp": 1729935189413,
          "uavModel": "M300"
      },
      "B30000111000": {
          "flightTime": 0,
          "flyType": "5",
          "gs": 47.08,
          "height": 45,
          "latitude": "22.5554",
          "longitude": "114.23217",
          "registerBody": "赛宝低空通航实验室",
          "snId": "B30000111000",
          "timeStamp": 1729935189412,
          "uavModel": "M300"
      }
    }
  */
  // 添加飞行后的路线
  async addPolyline() {
    __g.polyline.clear()
    console.log('这里清除')
    //自定义材质的折线
    let p2 = {
      id: FocusAirId.value + 'p', //折线唯一标识id
      coordinates: this.polylineArr, //构成折线的坐标点数组
      coordinateType: 0, //坐标系类型，取值范围：0为Projection类型，1为WGS84类型，2为火星坐标系(GCJ02)，3为百度坐标系(BD09)，默认值：0
      range: [1, 10000], //可视范围：[近裁距离, 远裁距离]，取值范围: [任意负值, 任意正值]
      thickness: 2, //折线宽度
      shape: 1, //折线类型 0：直线， 1：曲线  注意：设置为曲线非常影响添加效率
      depthTest: false, //是否做深度检测 开启后会被地形高度遮挡
      style: 4, //折线样式 参考样式枚举：PolylineStyle
      tiling: 0, //材质贴图平铺比例
      material: '/JC_CustomAssets/PolylineLibrary/Exhibition/混合线_4' //自定义材质路径 设置后style相关参数会失效
    }

    let pArr = []
    pArr.push(p2)
    await __g.polyline.add(pArr)
  }

  async addAir(air) {
    await __g.customObject.delete('air-' + air.snId)
    let newcoordinates = _turf.transform([Number(air.longitude), Number(air.latitude)])
    let m = {
      id: `air-` + air.snId, //maker唯一标识
      coordinate: [newcoordinates[0], newcoordinates[1], Number(air.height)], //坐标位置
      coordinateType: 0, //坐标系类型
      imagePath: window.origin + `/poi/无人机.png`, //图片路径
      anchors: [-25, 50],
      imageSize: [50, 50],
      fixedSize: true,
      textRange: [0, 20000000], //文本可视范围[近裁距离, 远裁距离]
      viewHeightRange: [0, 20000000], // 可见高度范围
      range: [0, 20000000], //标签的可见范围 [Min,Max]
      autoHidePopupWindow: true,
      groupId: 'wrjMarker',
      displayMode: 2, //显示模式
      autoHeight: false,
      occlusionCull: false
    }
    let o = {
      id: 'air-' + air.snId,
      groupId: 'wrjCustomObject',
      pakFilePath: HostConfig.PakPathWrj, //资源库pak文件路径,推荐使用cloud内置的文件资源管理器加载pak并使用@path方式传入参数
      assetPath: '/JC_CustomAssets/ObjectLibrary/Exhibition/交通工具/其他/无人机_1', //资源目录，自定义对象在pak文件资源包里的相对路径
      location: [newcoordinates[0], newcoordinates[1], Number(air.height)], //位置坐标
      coordinateType: 0, // 坐标系类型
      rotation: [0, 0, 0], // 世界坐标系旋转
      localRotation: [0, 90, 0], //模型自身旋转
      scale: [20, 20, 20], //模型缩放
      smoothMotion: 1, //1: 平滑移动，0: 跳跃移动
      supportAttach: true //不支持贴画贴合
    }
    if (air.flyType == 1) {
      //文旅观光
      m.imagePath = window.origin + `/poi/qjss_lenged_01.png`
      o.pakFilePath = HostConfig.PakPathHxs
      o.scale = [5, 5, 5]
      o.localRotation = [0, 0, 0]
      o.assetPath = '/JC_CustomAssets/ObjectLibrary/Exhibition/滑翔伞/滑翔伞带人'
    } else if (air.flyType == 2) {
      //载人通勤
      m.imagePath = window.origin + `/poi/qjss_lenged_02.png`
      o.pakFilePath = HostConfig.PakPath
      o.scale = [5, 5, 5]
      o.assetPath = '/JC_CustomAssets/ObjectLibrary/Exhibition/交通工具/飞机/直升机_1'
    } else if (air.flyType == 3) {
      console.log('%c [ air ]-110', 'font-size:13px; background:pink; color:#bf2c9f;', air)
      //物流运输
      o.scale = [5, 5, 5]
      o.assetPath = '/JC_CustomAssets/ObjectLibrary/Exhibition/盐田低空无人机/无人机2'
      m.imagePath = window.origin + `/poi/qjss_lenged_04.png`
      m.text = `起终点：保惠物流园-云海公园\n运营单位：云卓智能\n飞行器型号：云卓980S`
      m.fontColor = '#ffffff'
      m.fontSize = 8
      m.textBackgroundColor = [28 / 255, 81 / 255, 94 / 255, 0.8]
      m.range = [0, 2000000000] //标签的可见范围 [Min,Max]
      ;(m.popupOffset = [-150, 50]), //弹窗偏移
        (m.popupURL =
          window.origin +
          '/popup/zhishenji.html?parmas=' +
          encodeURIComponent(
            JSON.stringify({
              id: '亿航216s1',
              name: '亿航216s'
            })
          ))
    } else if (air.flyType == 4) {
      //政务巡检
      o.localRotation = [0, 0, 0]
      o.assetPath = '/JC_CustomAssets/ObjectLibrary/Exhibition/盐田低空无人机/无人机1'
      m.imagePath = window.origin + `/poi/qjss_lenged_03.png`
      m.text = `任务类型：应急巡查\n运营单位：高度创新\n飞行器型号：M190无人机`
      m.fontColor = '#ffffff'
      m.fontSize = 8
      m.textBackgroundColor = [28 / 255, 81 / 255, 94 / 255, 0.8]
      m.range = [0, 2000000000] //标签的可见范围 [Min,Max]
      ;(m.popupOffset = [-150, 50]), //弹窗偏移
        (m.popupURL =
          window.origin +
          '/popup/zhishenji.html?parmas=' +
          encodeURIComponent(
            JSON.stringify({
              id: '亿航',
              name: '飞行态势'
            })
          ))
    } else if (air.flyType == 5) {
      //飞行验证
      const uavType = air.uavType
      const pakFilePath = uavType === '1' ? HostConfig.PakPath : HostConfig.PakPathWrj
      const assetPath =
        uavType === '1'
          ? '/JC_CustomAssets/ObjectLibrary/Exhibition/交通工具/飞机/直升机_1'
          : '/JC_CustomAssets/ObjectLibrary/Exhibition/盐田低空无人机/无人机3'
      const imagePath =
        uavType === '1'
          ? window.origin + `/poi/qjss_lenged_02.png`
          : window.origin + `/poi/qjss_lenged_04.png`
      o.localRotation = uavType === '1' ? [0, 90, 0] : [0, -90, 0]
      o.scale = [5, 5, 5]
      o.pakFilePath = pakFilePath
      o.assetPath = assetPath
      m.imagePath = imagePath
    }
    this.airList.push(air.snId)
    await __g.customObject.add(o)
    //直升机需要调蓝图让翅膀转动
    if (['2', '5'].includes(air.flyType)) {
      __g.customObject.callBatchBPFunction({
        id: o.id,
        functionName: 'H_air anim'
      })
    }

    //模型上绑定标签
    __g.marker.add(m)
    __g.marker.setAttachCustomObject([
      {
        markerId: m.id, //标注id
        objectId: o.id, //自定义对象id
        offset: [0, 0, 0] //偏移量
      }
    ])
  }

  async startMove(newVal) {
    for (let i in newVal) {
      //循环新列表，如果航班已经被添加，则直接移动飞行，否则添加
      if (this.airList.includes(i)) {
        let localtion01 = _turf.transform([Number(newVal[i].longitude), Number(newVal[i].latitude)])
        let newLocation = [localtion01[0], localtion01[1], Number(newVal[i].height)]
        await __g.customObject.setLocation('air-' + newVal[i].snId, newLocation, 3)
        if (newVal[i].snId === FocusAirId.value) {
          this.polylineArr.push(newLocation)
          // this.addPolyline()
        }
      } else {
        this.addAir(newVal[i])
      }
    }
  }

  clearAirs() {
    this.airList = []
    __g.customObject.clear()
    __g.marker.clear()
  }

  // 清除飞行后的航线
  clerpolyline() {
    this.polylineArr = []
    console.log('这里清除')
    __g.polyline.clear()
  }
}

export default AirAnimation
